<link rel="stylesheet" href="__PUBLIC__/js/jquery-upload/css/jquery.fileupload.css" />
<link rel="stylesheet" href="__PUBLIC__/js/jquery-upload/css/jquery.fileupload-ui.css" />

<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">产品添加</h4>
        </div>
        <form style="padding:4%;height:500px;overflow-y:auto;" id="addGoodsForm" data-toggle="validator" role="form">
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品名称:</label>
                <input type="text" class="form-control" name="goods_name" value="{$goodsInfo.goods_name}" data-error="产品名称不能为空!" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品品牌:</label>
                <input type="text" class="form-control" name="brand" value="{$goodsInfo.brand}" data-error="产品品牌不能为空!" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品价格:</label>
                <input type="text" class="form-control" name="price" value="{$goodsInfo.price}" data-error="产品价格不能为空!" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品标签:</label>
                
                <div class="row">
                    <volist name="tagsList" id="gt">
                        <div class="col-xs-12 col-md-4">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name="tag[]" value="{$gt.tag_name}" <if condition="in_array($gt['tag_name'],$goodsInfo['goods_tags'])">checked="checked"</if> > {$gt.tag_name}
                                </label>
                            </div>
                        </div>
                    </volist>
                  </div>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品积分:</label>
                <input type="text" class="form-control" name="integral" value="{$goodsInfo.integral}" data-error="产品积分不能为空!" required>
                <div class="help-block with-errors"></div>
            </div>
			
			<div class="form-group">
                <label for="recipient-name" class="control-label">畅游产品一级目录:</label>
                <select name="cy_goods_type_1" class="form-control">
					<volist name="cy_goods_type" id="cgt">
						<option <if condition="$goodsInfo['cy_goods_type_1'] eq $cgt['key']">selected="selected"</if> value="{$cgt.key}">{$cgt.name}</option>
					</volist>
				</select>
            </div>
			
			<div class="form-group">
                <label for="recipient-name" class="control-label">畅游产品二级目录:</label>
                <select name="cy_goods_type_2" class="form-control">
					<volist name="cy_goods_type['000100']['sub_type']" id="cgt">
						<option <if condition="$goodsInfo['cy_goods_type_2'] eq $cgt['key']"></if> value="{$cgt.key}">{$cgt.name}</option>
					</volist>
				</select>
            </div>
			
			<div class="form-group">
                <label for="recipient-name" class="control-label">产品积分:</label>
                <input type="text" class="form-control" name="integral" value="{$goodsInfo.integral}" data-error="产品积分不能为空!" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">规格1:</label>
                <input type="text" class="form-control" name="spec_1" value="{$goodsInfo.spec_1}" >
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">规格2:</label>
                <input type="text" class="form-control" name="spec_2" value="{$goodsInfo.spec_2}" >
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品库存:</label>
                <input type="text" class="form-control" name="stock" value="{$goodsInfo.stock}" data-error="产品库存不能为空!" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品描述:</label>
                <textarea class="form-control" rows="5" name="goods_desc">{$goodsInfo.goods_desc}</textarea>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品内容:</label>
                <script id="container" name="goods_content" type="text/plain">
                
                </script>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品分类:</label>
                <select id="cateList" name="goods_type">
                    <volist name="cateList" id="cate">
                        <option <if condition="$goodsInfo['goods_type']['id'] eq $cate['id']"> selected="selected"</if> value="{$cate.id}" parent="{$cate.pid}"  >{$cate.type_name}</option>
                    </volist>
                </select>
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">产品图片:</label>
                <img id="userAvatarImg"  <if condition="$goodsInfo['goods_imgs'] neq '' " >src="__ROOT__{$goodsInfo.goods_imgs}" <else/> src="__PUBLIC__/images/avatar.png" </if>  width='150px' />
                <input id="fileupload" type="file" name="files[]" multiple style="display: none;">
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">状态:</label>
                <input type="checkbox" name="status" value="1" />
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">类型:</label>
                <input type="radio" id="radio_0" <if condition="$goodsInfo['type'] eq 0"> checked="checked" </if>  value="0" name="type" />
                <label for="radio_0">普通</label>
                <input type="radio" id="radio_1" <if condition="$goodsInfo['type'] eq 1"> checked="checked" </if> value="1" name="type" />
                <label for="radio_1">推荐</label>

                <input type="radio" id="radio_2" <if condition="$goodsInfo['type'] eq 2"> checked="checked" </if>  value="2" name="type" />
                <label for="radio_2">热销</label>
            </div>
            <input type="hidden" name="goods_imgs" id="goods_imgs" value="{$goodsInfo.goods_imgs}" />
            <input type="hidden" name="id" value="{$goodsInfo.id}" />
        </form>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
            <button type="button" id="userSave" class="btn btn-primary">保存</button>
        </div>
    </div>
</div>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/vendor/jquery.ui.widget.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload-ui.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/jquery-upload/js/jquery.fileupload-process.js"></script>

<script>
    var ROOTPATH = "__ROOT__";
    var newsContent = '{$goodsInfo.goods_content|htmlspecialchars_decode}';
	var cy_goods_type_json = {$cy_goods_type_json};
	
	
    $(function () {
        $("#addGoodsForm").validator();
        $("#cateList").select2tree();
		changeCyType();
		$("select[name='cy_goods_type_1']").change(changeCyType);
        Ue = UE.getEditor('container');
        
        Ue.ready(function() {
            Ue.setContent(newsContent);
        });
        var switchOption = {
            onText: "开",
            offText: "关",
            onColor: "success",
            offColor: "info",
            state: true,
            onSwitchChange: function (event, state) {
                if (state == true) {
                    $(this).val("1");
                } else {
                    $(this).val("0");
                }
            }
        };
        $("input:checkbox[name='status']").bootstrapSwitch(switchOption);

    })
	
	function changeCyType (){
	
		var type_1 = $("select[name='cy_goods_type_1']").val();
		
		var type_2_list = cy_goods_type_json[type_1].sub_type;
		
		var html = '';
		
		var type_2_value = '{$goodsInfo.cy_goods_type_2}';
		console.log(type_2_list);
		for(var i in type_2_list ){
			var item = type_2_list[i];
			var selected = '';
			if(item.key == type_2_value){
				selected = 'selected="selected"';
			}
			html += '<option '+selected+' value="'+item.key+'">'+item.name+'</option>';
		}
		console.log(html);
		$("select[name='cy_goods_type_2']").html(html);
		
	}

    $('input:radio').iCheck({
        checkboxClass: 'icheckbox_flat-blue',
        radioClass: 'iradio_flat-blue',
        increaseArea: '-10%'
    });

    $("#userAvatarImg").click(function(){
        $('#fileupload').click();
    })
    $('#fileupload').fileupload({
        url: "{:U('Admin/Upload/index')}",
        dataType: "",
        formData: {type: "goods"},
        autoUpload: true,
        done: function (e, data) {
            var files = data.result;
            var file = files[0];
            //console.log(file.filePath);
            $("#userAvatarImg").attr("src",ROOTPATH+file.filePath);
            $("#goods_imgs").val(file.filePath);
        }
    });

    $("#userSave").click(function () {
        var validator = $("#addGoodsForm").data("bs.validator");
        validator.validate();
        if(!validator.hasErrors()){
            $.ajax({
                url: "{:U('Admin/Goods/doAddGoods')}",
                type: "post",
                dataType: "json",
                data: $("#addGoodsForm").serialize(),
                success: function (data) {
                    if(data.status){
                        $("#commonModal").modal('toggle');
                        common.alert("提示", data.msg, function () {
                            common.reload();
                        });
                    }else{
                        common.alert("提示", data.msg)
                    }
                }
            })
        }
    })

    $('#commonModal').on('hidden.bs.modal', function (e) {
        //console.log(123);
        Ue.destroy();
        Ue = null;
    })
</script>